class MapTools {
  constructor(nav2d) {
      this.nav2d = nav2d;
      this.tool = 'select'; // 'select', 'waypoint', 'measure'
      
      this.initUI();
  }
  
  initUI() {
      document.getElementById('tool-select').addEventListener('click', () => {
          this.setTool('select');
      });
      
      document.getElementById('tool-waypoint').addEventListener('click', () => {
          this.setTool('waypoint');
      });
      
      document.getElementById('tool-measure').addEventListener('click', () => {
          this.setTool('measure');
      });
      
      document.getElementById('zoom-in').addEventListener('click', () => {
          this.nav2d.transform.scale *= 1.2;
          this.nav2d.render();
      });
      
      document.getElementById('zoom-out').addEventListener('click', () => {
          this.nav2d.transform.scale /= 1.2;
          this.nav2d.render();
      });
      
      document.getElementById('center-map').addEventListener('click', () => {
          this.nav2d.autoFitMap();
          this.nav2d.render();
      });
  }
  
  setTool(tool) {
      this.tool = tool;
      
      // 更新UI状态
      document.querySelectorAll('.tool-btn').forEach(btn => {
          btn.classList.remove('active');
      });
      document.getElementById(`tool-${tool}`).classList.add('active');
      
      // 更新鼠标样式
      this.nav2d.canvas.style.cursor = 
          tool === 'select' ? 'default' : 
          tool === 'waypoint' ? 'crosshair' : 'cell';
  }
  
  handleMapClick(x, y) {
      const worldPos = this.nav2d.screenToWorld(x, y);
      
      switch (this.tool) {
          case 'waypoint':
              this.nav2d.addWaypoint(worldPos.x, worldPos.y);
              break;
              
          case 'measure':
              this.showMeasurement(worldPos);
              break;
      }
  }
  
  showMeasurement(position) {
      if (!this.lastMeasurePoint) {
          this.lastMeasurePoint = position;
          return;
      }
      
      const dx = position.x - this.lastMeasurePoint.x;
      const dy = position.y - this.lastMeasurePoint.y;
      const distance = Math.sqrt(dx*dx + dy*dy).toFixed(2);
      
      alert(`Distance: ${distance} meters`);
      this.lastMeasurePoint = null;
  }
}